package cn.bs.ktv.connection;

import com.xuhao.didi.core.iocore.ReaderImpl;
import com.xuhao.didi.core.iocore.interfaces.IOAction;
import com.xuhao.didi.core.pojo.OriginalData;

import java.io.ByteArrayOutputStream;
import java.nio.charset.StandardCharsets;
import java.util.Objects;

public class KtvSocketReaderImpl extends ReaderImpl {
    private final String logName = KtvSocketReaderImpl.class.getName();

    private final ByteArrayOutputStream bodyStream = new ByteArrayOutputStream();

    private final int end_char_$ = 36;//$
    private final int end_char_r = 13;
    private final int end_char_n = 10;

    @Override
    public void read() throws RuntimeException {
        OriginalData originalData = new OriginalData();
        boolean headFlag = tryReadHead();
        if (headFlag) {
            bodyStream.reset();
        }

        int packageBytes = mOkOptions.getReadPackageBytes();
        boolean flag = tryReadBody(packageBytes);
        if (flag) {
            originalData.setBodyBytes(bodyStream.toByteArray());
            bodyStream.reset();
            mStateSender.sendBroadcast(IOAction.ACTION_READ_COMPLETE, originalData);
        }
    }

    private boolean tryReadHead() {
        byte[] headBytes = tryRead(1);
        if (headBytes == null) {
            return false;
        }

        boolean flag = headBytes[0] == end_char_$;
        if (!flag) {
            tryWriteBody(headBytes);
        }
        return flag;
    }

    private boolean tryReadBody(int length) {
        byte[] bodyBytes = tryRead(length);
        if (bodyBytes == null) {
            return false;
        }

        int bodyLength = bodyBytes.length;
        if (bodyLength <= length) {
            byte[] bs = new byte[3];
            bs[0] = bodyBytes[bodyLength - 3];
            bs[1] = bodyBytes[bodyLength - 2];
            bs[2] = bodyBytes[bodyLength - 1];

            if (bs[0] == end_char_$ && bs[1] == end_char_r && bs[2] == end_char_n) {
                tryWriteBody(bodyBytes, 0, bodyLength - 3);
                return true;
            }
        }

        tryWriteBody(bodyBytes);
        return false;
    }

    private String getString(byte[] bytes) {
        if (Objects.isNull(bytes) || bytes.length < 1) {
            return null;
        }
        return new String(bytes, StandardCharsets.UTF_8);
    }

    private byte[] tryRead(int length) {
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        try {
            byte[] buff = new byte[length];
            int len = mInputStream.read(buff);
            if (len > 0) {
                bos.write(buff, 0, len);
//                Log.d(logName, "the bos content is:" + getString(bos.toByteArray()));
                return bos.toByteArray();
            }
            return null;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    private void tryWriteBody(byte[] bytes) {
        tryWriteBody(bytes, 0, bytes.length);
    }

    private void tryWriteBody(byte[] bytes, int offset, int length) {
        if (Objects.isNull(bytes)) {
            return;
        }
        try {
            bodyStream.write(bytes, offset, length);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
